<template>
    <div>
        <Meta-Dialog v-model="isShow">
            <template #header><div>问题反馈</div> </template>

            <div class="max-h-[500px] overflow-y-auto">
                <div v-for="item in formData" class="p-[7px] mb-[15px]">
                    <div class="text-[16px]">
                        <span>{{ item.orderNumber }}</span
                        ><span v-if="item.orderNumber">.</span
                        >{{ item.LabelName }}
                    </div>
                    <div class="flex mt-[10px]">
                        <div
                            v-for="item2 in item.errorList"
                            class="border mr-[8px] px-[5px] py-[2px] cursor-pointer rounded-md"
                            :class="
                                item2.active
                                    ? 'bg-primary text-white border-primary'
                                    : 'bg-[#f2f2f2]'
                            "
                            @click="item2.active = !item2.active"
                        >
                            {{ item2.name }}
                        </div>
                    </div>
                    <div class="flex mt-[10px]">
                        <span>其他：</span
                        ><JrInput
                            v-model="item.OtherErrorContent"
                            class="flex-1"
                            size="small"
                            type="textarea"
                        ></JrInput>
                    </div>
                </div>
            </div>

            <div class="flex justify-end mt-[10px] mb-[5px] px-[7px]">
                <el-button size="small" @click="close">取消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="doSave"
                    :loading="saveLoading"
                    >保存</el-button
                >
            </div>
        </Meta-Dialog>
    </div>
</template>

<script setup>
import { SaveFeedbackListAsyncApi } from '@/api/home'
import { ElMessage } from 'element-plus'
import { inject, ref } from 'vue'

const isShow = ref(false)
const saveLoading = ref(false)
const zhinengtijianData = inject('zhinengtijianData')

const errorList = [
    {
        name: '体检结果错误',
        active: false
    },
    {
        name: '健康建议错误',
        active: false
    }
]

const errorList1 = [
    {
        name: '异常指标未检出',
        active: false
    },
    {
        name: '健康建议错误',
        active: false
    }
]

const formData = ref([])

const open = (data = []) => {
    if (data?.length) {
        formData.value = data.map((i) => {
            return {
                HealthCheckupKnowledgeID: zhinengtijianData.value.KnowledgeID,
                HealthCheckupKnowledgeVersionID:
                    zhinengtijianData.value.VersionID,
                HealthCheckupKnowledgeItemID: i.ID,
                LabelName: i.LabelName,
                errorList: JSON.parse(JSON.stringify(errorList)),
                OtherErrorContent: '',
                orderNumber: i.orderNumber
            }
        })
    } else {
        formData.value = []
    }
    formData.value.push({
        HealthCheckupKnowledgeID: zhinengtijianData.value.KnowledgeID,
        HealthCheckupKnowledgeVersionID: zhinengtijianData.value.VersionID,
        HealthCheckupKnowledgeItemID: 0,
        LabelName: '总检报告其他问题',
        errorList: errorList1,
        OtherErrorContent: ''
    })

    isShow.value = true
}

const close = () => {
    isShow.value = false
}

// 保存问题反馈
const doSave = () => {
    const haveOtherErrorContent = formData.value.some(
        (i) => i.OtherErrorContent
    )
    const haveLabelErrorContent = formData.value.some((i) =>
        i.errorList.some((j) => j.active)
    )
    if (!haveOtherErrorContent && !haveLabelErrorContent) {
        ElMessage.error('请选择或输入反馈问题')
        return
    }
    saveLoading.value = true
    const list = formData.value.map((i) => {
        return {
            HealthCheckupKnowledgeID: i.HealthCheckupKnowledgeID, // 知识库结果ID（主表ID）
            HealthCheckupKnowledgeVersionID: i.HealthCheckupKnowledgeVersionID, // 知识库版本ID
            HealthCheckupKnowledgeItemID: i.HealthCheckupKnowledgeItemID, // 知识库项目ID（明细项ID）
            LabelErrorContent: i.errorList.filter((i) => i.active).join(','), // 设定好的错误标签，多个用逗号隔开
            OtherErrorContent: i.OtherErrorContent // 其他错误信息（用户自由填写）
        }
    })
    SaveFeedbackListAsyncApi(list)
        .then((res) => {
            saveLoading.value = false
            if (res?.Code == '200') {
                ElMessage.success('提交反馈成功')
                close()
            } else {
                ElMessage.error(res?.Message)
            }
        })
        .catch((e) => {
            console.log(e)
            saveLoading.value = false
        })
}

defineExpose({
    open
})
</script>

<style scoped lang="scss"></style>
